package com.noah.note.presenter;

import com.google.gson.Gson;
import com.noah.note.R;
import com.noah.note.bean.Reply;
import com.noah.note.global.BasePresenter;
import com.noah.note.global.Const;
import com.noah.note.global.OkHttpCallback;
import com.noah.note.model.RegisterModel;
import com.noah.note.utils.NetUtils;
import com.noah.note.utils.ToastUtils;
import com.noah.note.view.RegisterView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class RegisterPresenter extends BasePresenter<RegisterView> {
    RegisterModel model;

    public RegisterPresenter() {
        model = new RegisterModel();
    }

    public void register(String username, String password, String name) {
        if (!NetUtils.isConnected()) {//无网络
            ToastUtils.show(R.string.failure_network);
            return;
        }
        model.register(username, password, name, new OkHttpCallback() {
            @Override
            public void onFail(Reply reply) {
                ToastUtils.show(R.string.failure_register);
            }

            @Override
            public void onSuccess(Reply reply) {
                int code = reply.getCode();
                ToastUtils.show(reply.getMsg());
                if (code == Const.SUCCESS) {//注册成功
                    if (getView() != null) {
                        getView().finishActivity();
                    }
                }
            }
        });
    }
}
